function init() {
	$(".selector").each(function() {
		 $(this).selectorInit();
	});
	var cur_idx = null;
	JCalendar.click = function(obj){
		JCalendar.cur_date = parseInt(obj.innerHTML);
		if (JCalendar.onclick) {
			var dt = new Date(JCalendar.cur_year, JCalendar.cur_month - 1, JCalendar.cur_date);
			JCalendar.onclick(JCalendar.cur_year,JCalendar.cur_month,JCalendar.cur_date);
		}
	}
	JCalendar.onclick = function(year, month, date) {
		var daystr = year + "-" + month + "-" + date;
		if(layer){
			layer.hide();
		}
        if(cur_idx == 'start') {
            var checktime = $("#end").val();
            if(CompareDate(daystr,checktime)) {
                alert('开始时间不允许大于结束时间');return false;
            }
        } else {
            var checktime = $("#start").val();
            if(CompareDate(checktime,daystr)) {
                alert('开始时间不允许大于结束时间');return false;
            }
        }
        $("#"+cur_idx).val(daystr);
	}
	var layer = $("<div></div>").css({
		"display": "none",
		"position": "absolute",
		"z-index": "2010"
	}).html(new JCalendar().show()).appendTo(document.body);
	$(".click_calendar").click(function() {
        $("#noalltime").attr('checked',true);
		layer.show();
		var pos = $(this).offset();
		layer.css({
			"left": pos.left,
			"top": pos.top
		});
        cur_idx = $(this).attr('type');
		return false;
	});
	if('title' == $("select[name='s_type']").val()){
		$("#name_search_type").show();
	}else{
		$("#name_search_type").hide();
	}
	$("select[name='s_type']").change(function(){
		if('title' == $("select[name='s_type']").val()){
			$("#name_search_type").show();
		}else{
			$("#name_search_type").hide();
		}
	});
}
function CompareDate(d1,d2)
{
  return ((new Date(d1.replace(/-/g,"\/"))) > (new Date(d2.replace(/-/g,"\/"))));
}

$(function() {
    init();
    sch_api();
  
});

function getConds()
{
	//获取各查询条件
    var platform = $('select[name="platform"]').val();	//所属平台ID
    var module = $('select[name="module"]').val();	//所属模块ID
    var apitype = $('select[name="type"]').val(); //接口类型
    var s_type = $('select[name="s_type"]').val();	//查询类型
    var s_value = $('input[name="s_value"]').val();	//查询内容
    var stime = $('input[name="stime"]').val();	//开始时间
    var etime = $('input[name="etime"]').val();	//结束时间
    var curuser = $('input[name="developer"]').val();	//当前登录人员
    var need_edit = $('input[name="need_edit"]').val();	//是否未入库接口页面

    var datastr = '';
    var apiclass = 0;
    
    if(1==need_edit){
    	datastr += '&need_edit=' + need_edit;
    }else{
    	$('a[name="apiclass"]').each(function(){	//接口分类
        	if($(this).attr('class') == 'tablet_set'){
        		apiclass = $(this).attr('rel');
        		if(apiclass != -1){
        	    	datastr += '&class=' + apiclass;
        	    }
        	}
        });
       
        $('a[name="apistatus"]').each(function(){	//接口状态
        	if($(this).attr('class') == 'tablet_set'){
        		var apistatus = $(this).attr('rel');
        		if(apistatus != -1){
        	    	datastr += '&status=' + apistatus;
        	    }
        	}
        });
        
        if(platform != 0){
        	datastr += '&platform=' + platform;
        }
        if(module != 0){
        	datastr += '&module=' + module;
        }
        if(apitype != 0){
        	datastr += '&type=' + apitype;
        }
        if(curuser){
        	datastr += '&curuser=' + curuser;
        }
        datastr += '&s_type=' + s_type;
        datastr += '&s_value=' + s_value;    
        if('title' == s_type){
        	datastr += '&name_search_type=' + $("input[name='name_search_type']:checked").val();
        }
    }    
    datastr += '&stime=' + stime;
    datastr += '&etime=' + etime;
    return datastr;
}

function sch_api(pagenum){
	$('.page_selector').html('');
	$('#list').html('');
	var datastr = getConds();
	var pagenum = parseInt(pagenum);
	if(pagenum)
	{
		datastr += '&page=' + pagenum;
	}
	showCommonLayer("数据获取中……",3);
	$.ajax({
        type: "POST",
        url: "/apinew/aj_api.php",
        data: "action=index"+datastr,
        dataType: "json",
        success: function(json){
        	common_click();
        	var html = '';
            if(json.curtotal >0) {
            	$('.common_table').show();
            	$('.page_selector').show();
            	$('.PY_clew').hide();
            	$('.PY_clewcon').hide();

				for(i=0; i<json.curtotal; i++)
				{
					html += '<tr>';
					html += '<td>' + json.list[i]['id'] + '</td>';
					if(1 != json.need_edit){
						html += '<td><a href="apidetail.php?id='+json.list[i]['id']+'">' + json.list[i]['title'] + '</a></td>';
					}				
					html += '<td>' + json.list[i]['url'] + '</td>';
					if(1 != json.need_edit){
						html += '<td>' + json.list[i]['platform_name'] + '</td>';
						//html += '<td>' + json.list[i]['type_name'] + '</td>';
						html += '<td>' + json.list[i]['class_name'] + '</td>';
						html += '<td>' + json.list[i]['developer'] + '</td>';
					}
					html += '<td>' + json.list[i]['ctime'] + '</td>';
					html += '<td>' + json.list[i]['uptime'] + '</td>';
					if(1 != json.need_edit){
						if(1 == json.list[i]['ch_status_power']){
							html += '<td><a href="javascript:void(0);" name="ch_status" rel="' + json.list[i]['id'] + '" status="'+json.list[i]['status']+'" developer="' + json.list[i]['developer'] + '">' + json.list[i]['status_name'] + '</a></td>';
						}else{
							html += '<td>' + json.list[i]['status_name'] + '</td>';
						}
						if(1 == json.list[i]['edit_power']){
							html += '<td><a href="apiedit.php?id=' + json.list[i]['id'] + '">修改</a></td>';
						}else{
							html += '<td><span class="gray">修改</span></td>';
						}
					}else{
						html += '<td>' + json.list[i]['status_name'] + '</td>';
						html += '<td><a href="apiedit.php?id=' + json.list[i]['id'] + '&need_edit=1">编辑</a><span class="split"></span><a href="javascript:void(0);" class="insert_into" rel="' + json.list[i]['id'] + '" rel_url="' + json.list[i]['url'] + '">入库</a></td>';
					}				
					html += '</tr>';
				}
				$('.page_selector').html(json.pagehtml);

				$('.btn_num').click(function(){
					sch_api($(this).attr('rel'));
				});
				$('#list').html(html);
				$('a[name="ch_status"]').click(function(){
					var obj = $(this);
					var status = obj.attr('status');
					var developer = obj.attr('developer');
		            var id = obj.attr('rel');
					var tiptxt = '';
					var to_status = 1;
					if(2 == status){
						tiptxt = '激活';
					}else if(1 == status){
						tiptxt = '冻结';
						to_status = 2;
					}
					var datastr = '';
					    datastr += '&id='+id;
					    datastr += '&status=' + status;
					    datastr += '&to_status=' + to_status;
					    datastr += '&developer=' + developer;
					    showCommonLayer("确定要" + tiptxt + "此接口吗？", 2, function(){
						showCommonLayer("处理中。。。",3);
				        $.ajax({
				            type: "POST",
				            url: "/apinew/aj_api.php",
				            data: "action=del"+datastr,
				            dataType: "json",
				            success: function(json){
								if(json.status) {
									common_click();
									$('a[name="ch_status"][rel="'+id+'"]').attr('status', to_status);
									$('a[name="ch_status"][rel="'+id+'"]').html(tiptxt);
									showCommonLayer("操作成功！", 4);
				                } else {
				                	showCommonLayer(json.msg, 5);
				                }
				            }
				        });
			 		});
				});
				//未入库接口入库
			    $(".insert_into").click(function(){
			    	var id = $(this).attr("rel");
			    	var interface_url = $(this).attr("rel_url");
			    	var url = '/apinew/aj_api.php';
			    	var post = {action:'insert', id:id, interface_url:interface_url};
			    	showCommonLayer('确定要入库吗？', 2, function(){
			    		$.post(url, post, function(json){
				    		if(1 == json.status){
				    			showCommonLayer("入库成功", 4);	
				    			window.location.reload();
				    		}else{
				    			showCommonLayer(json.msg, 5);
				    		}
				    	}, 'json');
			    	});
			    	
			    });
				$('body').trigger('click');
            } else {
            	$('.page_selector').html('');
            	$('.common_table').hide();
            	$('.page_selector').hide();
            	$('.PY_clew').show();
            	$('.PY_clewcon').show();
            }
        }
    }); 
	
}

$('body').click(function(){
	$('#overlay').css('width','50px;');
});

function searchByClass(class_id)
{
	$('a[name="apiclass"]').parent().attr('class', '');
	$('a[name="apiclass"][rel="'+class_id+'"]').parent().attr('class', 'current');
	$('a[name="apiclass"]').attr('class', 'tablet_unset');
	$('a[name="apiclass"][rel="'+class_id+'"]').attr('class', 'tablet_set');
	sch_api();
}

function searchByStatus(status_id)
{
	$('a[name="apistatus"]').parent().attr('class', '');
	$('a[name="apistatus"][rel="'+status_id+'"]').parent().attr('class', 'current');
	$('a[name="apistatus"]').attr('class', 'tablet_unset');
	$('a[name="apistatus"][rel="'+status_id+'"]').attr('class', 'tablet_set');
	sch_api();
}